/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chelydra.sharelydra.client.message;

import chelydra.sharelydra.client.Core;
import java.io.Serializable;

/**
 * Classe modeling the packet send from client to client. All the differents
 * others packets extend this one. The classe is Serializable to be sent
 * through the networks.
 * See the following file for more information
 * http://sharelydra.googlecode.com/svn/Documents/Protocol%20Description/Client_to_Client_protocol_description_.pdf
 * The new type of the messages are int, and not byte because of the way
 * Java handles byte.
 * @author Benjamin
 */
public class Message {

    //Version of the protocol used
    int version;
    //Type of message exchanged
    int type;
    //Data of the message
    String data;

    public Message() {
    }

    public Message( int type, String data) {
        this.setVersionToDefault();
        this.type = type;
        this.data = data;
    }

    public Message(Message message){
        this.version=message.version;
        this.type=message.type;
        this.data=message.data;
    }

    public Message(int type) {
        this.setVersionToDefault();
        this.type = type;
    }

    public void setVersionToDefault(){
        this.version=Core.VersionMessage;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

	@Override
    public String toString(){
        String result= "";
        result=result+Integer.toString(this.version)+"#"+Integer.toString(this.type)+"#"+this.data+"\n";
        return result;
    }

    /*
    * Used to unserialize the packet from a String recieved through the Network
    */
    public void fromString(String str){
        int firstSharpPos=str.indexOf("#");
        int secondSharpPos=str.indexOf("#",firstSharpPos+1);
        this.version=Integer.parseInt(str.substring(0, firstSharpPos));
        this.type=Integer.parseInt(str.substring(firstSharpPos+1,secondSharpPos));
        this.data=str.substring(secondSharpPos+1,str.length());
    }


}
